<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2019/7/17
 * Time: 8:17
 */

namespace app\admin\controller;


use think\Db;

class Topomap extends Common
{
    public function topomap(){
        // $house = Db::name('nyd_house')->select();
        // foreach ($house as $k=>$v){
        //     $children1= $house[$k]['id'];
        //     $sql="select id, gateway_id  ,gateway_name as name,house_id   from nyd_gateway where house_id=".$children1;
        //     $arr1 = Db::name('nyd_gateway')->query($sql);
        //
        //    foreach ($arr1 as $key=>$value)
        //    {
        //        $house_id = $arr1[$key]['house_id'];
        //        $sql="select id, control_id ,control_longname as name  from nyd_control_dev where   gateway_id='".$value['id']."'";
        //        $arr_kongzhiqi = Db::name('nyd_control_dev')->query($sql);
        //        foreach ($arr_kongzhiqi as $item=> $vo){
        //
        //            $sql="select id, dev_mac ,dev_name as name,control_id  from nyd_dev where control_id='".$vo['id']."'";
        //            $arr_shebei = Db::name('nyd_dev')->query($sql);
        //            // if($arr_kongzhiqi[$item]['id'] == $arr_shebei[])
        //            // echo '<pre>';
        //            // print_r($arr_kongzhiqi);
        //            // echo '<pre>';
        //            // print_r($arr_shebei);
        //            $arr_kongzhiqi[$item]["children"]=$arr_shebei;
        //
        //        }
        //
        //        $arr1[$key]["children"]=$arr_kongzhiqi;
        //        $house[$k]['children']=$arr1;
        //        $aa["children"]=$house;
        //
        //        // $aa["id"]=0;
        //        // $aa["name"]="王朋阳";
        //        $bb[]=$aa;
        //        $aaa["data"]=$bb;
        //        $a=json_encode($aaa);
        //        // echo '<pre>';
        //        // print_r($a);
        //
        //    }
        //
        // }
        $house = Db::name('nyd_house')->field('id,house_name as name,1 as cid')->select();
        $gateway = Db::name('nyd_gateway')->field('id,house_id,is_open,gateway_id,gateway_name as name,2 as cid')->where('is_bad',1)->select();
        $sensor = Db::name('nyd_sensor')->field('id,sensor_mac,sensor_name as name,is_open,gateway_id, 3 as cid')->where('is_del',1)->where('is_bad',1)->select();
        $control = Db::name('nyd_control_dev')->field('id,is_open,control_longname as name,control_id,gateway_id,3 as cid')->where('is_del',1)->where('is_bad',1)->select();
        $dev = Db::name('nyd_dev')->field('id,dev_name as name,is_open,dev_mac,control_id,4 as cid')->where('is_del',1)->where('is_bad',1)->select();

        // 关联控制器
        foreach ($control as $key => $value){

            $control[$key]['children'] = [];

            foreach ($dev as $k => $v){

                if($value['id'] == $v['control_id']){
                    array_push($control[$key]['children'],$v);
                }

            }

        }
        // P($control);
        $control = array_merge($control,$sensor);


        // 关联网关
        foreach ($gateway as $key => $value){

            $gateway[$key]['children'] = [];

            foreach ($control as $k => $v){

                if($value['id'] == $v['gateway_id']){
                    array_push($gateway[$key]['children'],$v);
                }

            }

        }

        // 关联大棚
        foreach ($house as $key => $value){

            $house[$key]['children'] = [];

            foreach ($gateway as $k => $v){

                if($value['id'] == $v['house_id']){
                    array_push($house[$key]['children'],$v);
                }

            }

        }


       $aa["children"]=$house;

       $bb[0]=$aa;
       $aaa["data"]=$bb;
       $a=json_encode($aaa);

        $this->assign("data",$a);
        return $this->fetch('topomap/index');
    }

}